﻿unit HostControlPanel;

{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}

interface

uses
{$IFNDEF FPC}
  Windows,
{$ELSE}
  LCLIntf, LCLType, LMessages,
{$ENDIF}
  Messages, SysUtils, Graphics, Controls, Forms, Dialogs, MsgBaseForm, unitCommonDataStruct, unitCommonTypes,
  StdCtrls, Classes, unitSvrMngCommon, unitClientBuffer, unitCommonClasses, Buttons, unitUniqueDataCommon,
  unitFileAndTransCommon, unitMisc;

type
  TfrmHostControlPanel = class(TfrmMsgBase)
    Label1: TLabel;
    mHostInfo: TMemo;
    sbHostParams: TScrollBox;
    lLastUpdate: TLabel;
    eLastUpdate: TEdit;
    Label2: TLabel;
    eFilePath: TEdit;
    bBrowser: TBitBtn;
    Label3: TLabel;
    eDuration: TEdit;
    Label4: TLabel;
    eMainProcVer: TEdit;
    procedure bBrowserClick(Sender: TObject);
  private
    { Private declarations }
    procedure ParamRecordUpdated(Sender: TObject; ParObj, obj: TSerialObj; const sid, uid: TID_OBJ);
    procedure ParamRecordDeled(Sender: TObject; obj: TSerialObj; const sid, uid: TID_OBJ);
    procedure AdjustParamPos;
    procedure ParamClicked(Sender: TObject);
  public
    { Public declarations }
    procedure FuncStart; override;
    procedure FuncStop; override;
  end;

implementation

uses unitObjPool, unitWinAPI;

{$IFNDEF FPC}
{$R *.dfm}
{$ELSE}
{$R *.lfm}
{$ENDIF}
{ TfrmHostControlPanel }

procedure TfrmHostControlPanel.AdjustParamPos;
var
  t: integer;
  i: integer;
  j: integer;
begin
  t := 10;
  for i := 0 to sbHostParams.ControlCount - 1 do
    if sbHostParams.Controls[i].ClassType = TLabel then
    begin
      TLabel(sbHostParams.Controls[i]).Top := t;
      for j := i + 1 to sbHostParams.ControlCount - 1 do
        if (sbHostParams.Controls[j].ClassType = TBitBtn) and
          (sbHostParams.Controls[i].tag = sbHostParams.Controls[j].tag) then
        begin
          TBitBtn(sbHostParams.Controls[j]).Top := t;
          Break;
        end;
      Inc(t, 35);
    end;
end;

procedure TfrmHostControlPanel.bBrowserClick(Sender: TObject);
var
  nv: TCommonNameValue;
  sa: TCommStrArr;
begin
  sa := GetHostFilesDlg(Self, True);
  if (Length(sa) > 0) and (sa[0] <> '') then
  begin
    nv := ObjPool.GetObj(TCommonNameValue, Self);
    nv.ObjID := ID_DOWNLOADPATH;
    nv.Value := sa[0];
    nv.ValueType := vtString;
    AddRecord(PeerHost, ID_HOSTPARAMTABLE, nv);
  end;
end;

procedure TfrmHostControlPanel.FuncStart;
var
  s: string;
begin
  inherited;
  if PeerHost = HOSTID_ROOT then
  begin
    lLastUpdate.Visible := False;
    eLastUpdate.Visible := False;
  end;
  AddBindInfo(ID_HOSTPARAMTABLE, PeerHost, nil, nil, nil, nil, nil, nil, ParamRecordUpdated, ParamRecordDeled);
end;

procedure TfrmHostControlPanel.FuncStop;
begin
  inherited;
end;

procedure TfrmHostControlPanel.ParamClicked(Sender: TObject);
var
  nv, nvt: TCommonNameValue;
begin
  nv := Pointer(TBitBtn(Sender).tag);
  if nv.ValueType in [vtSwitch, vtFunc] then
  begin
    nvt := TCommonNameValue(nv.Clone);
    if nvt.Value = S_SWITCH_ON then
      nvt.Value := S_SWITCH_OFF
    else
      nvt.Value := S_SWITCH_ON;
    AddRecord(PeerHost, ID_HOSTPARAMTABLE, nvt);
  end;
end;

procedure TfrmHostControlPanel.ParamRecordDeled(Sender: TObject; obj: TSerialObj; const sid, uid: TID_OBJ);
var
  i: integer;
begin
  for i := sbHostParams.ControlCount - 1 downto 0 do
    if Pointer(sbHostParams.Controls[i].tag) = obj then
      sbHostParams.Controls[i].Free;
end;

procedure TfrmHostControlPanel.ParamRecordUpdated(Sender: TObject; ParObj, obj: TSerialObj; const sid, uid: TID_OBJ);
var
  i: integer;
  nv: TCommonNameValue absolute obj;
  l: TLabel;
  b: TBitBtn;
  d: TDateTime;

  procedure SetBtnCaption(btn: TBitBtn);
  begin
    case nv.ValueType of
      vtSwitch:
        if nv.Value = S_SWITCH_ON then
          btn.Caption := S_SWITCH_CAPTION_ON
        else
          btn.Caption := S_SWITCH_CAPTION_OFF;
      vtFunc:
        btn.Caption := '执行';
    end;
  end;

begin
  if obj.ObjID = ID_DEVICEINFO then
    mHostInfo.Text := nv.Value
  else if obj.ObjID = ID_SYSDURATION then
    eDuration.Text := SecondToDateStr(StrToInteger64(nv.Value), True)
  else if obj.ObjID = ID_DOWNLOADPATH then
    eFilePath.Text := nv.Value
  else if obj.ObjID = ID_MAINPROCVER then
    eMainProcVer.Text := nv.Value
  else if obj.ObjID = ID_LASTUPDATE then
  begin
    if nv.Value <> '' then
    begin
      d := StrToFloat(nv.Value);
      eLastUpdate.Text := DateTimeToStr(d);
    end;
  end
  else if nv.ValueType in [vtSwitch, vtFunc] then
  begin
    for i := 0 to sbHostParams.ControlCount - 1 do
      if Pointer(sbHostParams.Controls[i].tag) = obj then
      begin
        if sbHostParams.Controls[i].ClassType = TLabel then
          TLabel(sbHostParams.Controls[i]).Caption := nv.Name
        else if sbHostParams.Controls[i].ClassType = TBitBtn then
        begin
          SetBtnCaption(TBitBtn(sbHostParams.Controls[i]));
          Exit;
        end;
      end;
    l := TLabel.Create(sbHostParams);
    l.Caption := nv.Name;
    l.Left := 10;
    l.Parent := sbHostParams;
    l.tag := NativeUInt(obj);
    b := TBitBtn.Create(sbHostParams);
    SetBtnCaption(b);
    b.Parent := sbHostParams;
    b.Left := sbHostParams.ClientWidth - 10 - b.Width;
    b.tag := NativeUInt(obj);
    b.Anchors := [akTop, akRight];
    b.OnClick := ParamClicked;
    AdjustParamPos;
  end;
end;

initialization

RegFuncClass(HOSTID_ALLMASK, '主机控制面板', '提供主机功能操作界面', TfrmHostControlPanel);

end.
